<?php
    header("Access-Control-Allow-Origin:*");    // 响应类型
    header('Access-Control-Allow-Methods:POST');    // 响应头设置
    header('Access-Control-Allow-Headers:x-requested-with, content-type');
    header("Content-type: text/html; charset=utf-8");
	require ('config.php');
	
	
    $name = $_POST['name'];
    $fid = $_POST['fid'];
	
    $fileSize = $_FILES["file"]["size"];
    
	$validWidPattern = '/^\d+$/';
	if (!preg_match($validWidPattern, $fid) and $fid!=NULL)  {
		echo json_encode(array('stat' => '400','msg' => '非法请求，GET数据错误！'),JSON_UNESCAPED_UNICODE);
	} 
    
	$name = str_ireplace(['"', '<', '>','select',"'"], ['', '&lt;', '&gt;','禁止注入!',"&apos;"], $name);  

    if($fileSize==0 or $name==NULL or $fid==NULL){
        echo json_encode(array('stat' => '400','msg' => '失败！木有图片或姓名上传'),JSON_UNESCAPED_UNICODE);
    }
    else{
        $fid = $fid+1-1;
        $sql = "SELECT list,pax,uid,class,stoptime FROM gather WHERE fid LIKE ".$fid;
        $gather = mysqli_query($conn,$sql);
        $gather = mysqli_fetch_array($gather);
        if($gather){
            $list = $gather['list'];
            $pax = $gather['pax'];
            $uid = $gather['uid'];
            $class = $gather['class'];

            $stoptime = $gather['stoptime'];
            $time1 = substr($stoptime,0,10).' '.substr($stoptime,11,5);
			date_default_timezone_set('PRC');
            $time2=date("Y-m-d H:i");
            
            if($time1 <= $time2){
                $stoptime = '收集表已过期';
                echo json_encode(array('stat' => '400','msg' => '收集表已过期，无法再进行提交！'),JSON_UNESCAPED_UNICODE);
            }else{
                
                //查询是否存在此文件夹
                $dir='../upfile/'.$fid;
                if(is_dir($dir)){}else{
                    mkdir($dir,0777,true);    //创建文件夹
                }

                //对象是否存在
                if($class=='其他'){
                    $filename = $dir.'/'.$name.'.png';
                }else{
                    $filename = $dir.'/'.$class.$name.'.png';
                }

                //开始存储文件
                $temp = explode(".", $_FILES["file"]["name"]);
                $extension = end($temp);    
                //小于10MB
                if ($_FILES["file"]["size"] < 10240000)
                {   //转存文件
                    move_uploaded_file($_FILES["file"]["tmp_name"], $filename);
                    
                    if (strpos($list, $name) !== false) {   //检查是覆盖还是新提交的
                        echo json_encode(array('stat' => '200','msg' => '已为你更新了截图'),JSON_UNESCAPED_UNICODE);
                    }else{  
                        $pax = $pax+1;
                        $list = $list.' '.$name;
                        $tw =  '上传成功，你是第'.$pax.'位上传的用户';
                        echo json_encode(array('stat' => '200','msg' => $tw),JSON_UNESCAPED_UNICODE);

                        //更新数据库列表
                        $sql= "UPDATE gather SET list='$list',pax=$pax WHERE fid = '$fid'";
                        $result=$conn->query($sql);
                        if(!$result){
                            echo json_encode(array('stat' => '400','msg' => '数据更新失败，请重试尝试！'),JSON_UNESCAPED_UNICODE);
                        }
                    }
                }
            }
        }else{
            echo json_encode(array('stat' => '400','msg' => '你操作的收集表不存在！'),JSON_UNESCAPED_UNICODE);
        }
        
        
    }
    

?>